﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CooStationsToolsNew
{
    public static class FileOP
    {
        /// <summary>
        /// 将字节流写入文件
        /// </summary>
        public static void WriteFile(byte[] bs, string file)
        {
            //将数组写入文件
            CreatFolder(file);
            Stream writer = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Write);
            writer.Write(bs, 0, bs.Length);
            writer.Flush();
            writer.Close();
        }

        /// <summary>
        /// 将文件以字节流读出
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static byte[] ReadFile(string file)
        {
            FileStream fs = File.Open(file, FileMode.Open);
            byte[] bss = new byte[fs.Length];
            int i = fs.Read(bss, 0, (int)fs.Length);
            fs.Dispose();
            fs.Close();
            return bss;
        }
        /// <summary>
        /// 删除指定文件
        /// </summary>
        /// <param name="file"></param>
        public static void DeleteFile(string file)
        {
            try
            {
                System.IO.File.Delete(file);
            }
            catch { }
        }
        /// <summary>
        /// 获取一个文件夹下的所有文件
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public static string[] GetFileSystemEntries(string dir)
        {
            dir = Path.GetDirectoryName(dir);
            return Directory.GetFileSystemEntries(dir);
        }

        /// <summary>
        /// 复制文件夹
        /// </summary>
        /// <param name="sourceFolder">要复制的文件夹</param>
        /// <param name="destFolder">目标文件夹</param>
        public static void CopyFolder(string sourceFolder, string destFolder)
        {
            if (!Directory.Exists(destFolder))
            {
                Directory.CreateDirectory(destFolder);
            }
            string[] files = Directory.GetFiles(sourceFolder);
            foreach (string file in files)
            {
                string name = Path.GetFileName(file);

                string dest = Path.Combine(destFolder, name);

                File.Copy(file, dest);
            }
            string[] folders = Directory.GetDirectories(sourceFolder);
            foreach (string folder in folders)
            {
                string name = Path.GetFileName(folder);

                string dest = Path.Combine(destFolder, name);

                CopyFolder(folder, dest);
            }
        }

        /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="dir"></param>
        public static void DeleteFolder(string dir)
        {
            if (Directory.Exists(dir)) //如果存在这个文件夹删除之 
            {
                foreach (string d in Directory.GetFileSystemEntries(dir))
                {
                    if (File.Exists(d))
                        File.Delete(d); //直接删除其中的文件 
                    else
                        DeleteFolder(d); //递归删除子文件夹 
                }
                Directory.Delete(dir); //删除已空文件夹 

            }
        }
        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static bool ExistsFile(string file)
        {
            return System.IO.File.Exists(file);
        }

        /// <summary>
        /// 创建文件目录
        /// </summary>
        /// <param name="folder"></param>
        public static void CreatFolder(string folder)
        {
            folder = System.IO.Path.GetDirectoryName(folder);
            if (!System.IO.Directory.Exists(folder))
                System.IO.Directory.CreateDirectory(folder);
        }

    }
}
